import {Component, OnInit, ViewChild, Input, Output, EventEmitter} from '@angular/core';
import {ModalDirective} from 'ngx-bootstrap/modal';
import {UserModel} from "../../model/user.model";
import {SMS} from "../../service/sms.service";
import {mobileValidator, passwordValidator, equalValidator, verifyValidator} from "../../utils/validator.utils";
import {FormGroup, FormControl, FormBuilder, Validators} from "@angular/forms";
import {ROUTE} from "../../service/route.service";
import {WfToast} from "../../utils/toast.utils";
import {USERAPI} from "../../service/user.api.service";
import {QgyProtocolModalComponent} from "../qgy-protocol-modal/qgy-protocol-modal.component";

@Component({
  selector: 'login-modal-view',
  templateUrl: './login-modal-view.component.html',
  styleUrls: ['./login-modal-view.component.scss']
})
export class LoginModalViewComponent implements OnInit {

  @ViewChild(QgyProtocolModalComponent) public protocolModal:any;
  @ViewChild('loginModal') public loginModal:ModalDirective;
  @Input('showModal') public isModalShown:boolean = false;
  @Input('modalContent') public modalContent:string = 'login';
  @Output('modalClose') public modalClose = new EventEmitter();
  isPsdLogin:boolean = true;
  isVerCodeLogin:boolean = false;
  saveCookie:boolean = false;
  getVerTitle:string = '获取验证码';
  num:number = 60;
  disabled:boolean = false;
  timer:any = null;
  login = {username: '', password: ''};
  verlogin = {username:'',verify:''};
  register = {username: '', verify: '', password: '', pwdChk: ''};
  loginForm:FormGroup;
  verloginForm:FormGroup;
  registerForm:FormGroup;

  constructor(private userApi:USERAPI,
              private SMS:SMS,
              private fb:FormBuilder,
              private route:ROUTE,
              private toast:WfToast) {
    this.loginForm = fb.group({
      username: ['', mobileValidator],
      password: ['', passwordValidator]
    });
    this.verloginForm = fb.group({
      username: ['',mobileValidator],
      verifyCode: ['',verifyValidator]
    });
    this.registerForm = fb.group({
      username: ['', mobileValidator],
      verifyCode: ['', verifyValidator],
      password: ['', passwordValidator],
      pwdCheck: ['']
    }, equalValidator);
  }
  showPsdLogin(){
    this.isVerCodeLogin = false;
    this.isPsdLogin = true;
  }
  showVerCodeLogin(){
    this.isPsdLogin = false;
    this.isVerCodeLogin = true;
  }
  doLogin() {
    let reqData = {
      "communityId": "2f93a76eda6c4873811befac41c9c0c2", //平台的uuid
      "username": this.loginForm.get('username').value,
      "password": this.loginForm.get('password').value,
    };
    let close=this.modalClose;
    this.userApi.login(reqData, (resp:any)=>{
        close.emit(new UserModel(resp));
        this.toast.msg('登录成功,刷新页面可以获得更多信息！');
    });
  }

  doVerLogin(){
    let reqData = {
      "communityId": "2f93a76eda6c4873811befac41c9c0c2", //平台的uuid
      "phone": this.verloginForm.get('username').value,
      "verifyCode": this.verloginForm.get('verifyCode').value
    };
    let close=this.modalClose;
    this.userApi.loginByVerifyCode(reqData, (resp:any)=>{
        close.emit(new UserModel(resp));
        this.toast.msg('登录成功,刷新页面可以获得更多信息！');
    });
  }

  doLoginVerify(){
    this.SMS.send(this.verloginForm.get('username').value, (data:any)=>console.log("sms", data))
    this.disabled = true;
    this.timer = setInterval(()=>{
      this.num--;
      this.getVerTitle = this.num + '秒';
      if(this.num===0){
        clearInterval(this.timer);
        this.disabled = false;
        this.num = 60;
        this.getVerTitle = '获取验证码';
      }
    },1000);

  }


  doVerify() {
    this.SMS.send(this.registerForm.get('username').value, (data:any)=>console.log("sms", data));
    this.disabled = true;
    this.timer = setInterval(()=>{
      this.num--;
      this.getVerTitle = this.num + '秒';
      if(this.num===0){
        clearInterval(this.timer);
        this.disabled = false;
        this.num = 60;
        this.getVerTitle = '获取验证码';
      }
    },1000);

  }

  doRegister() {
    let reqData = {
      "communityId": "2f93a76eda6c4873811befac41c9c0c2", //平台的uuid
      "phone": this.registerForm.get('username').value,
      "verifyCode": this.registerForm.get('verifyCode').value,
      "password": this.registerForm.get('password').value,
      "pwdCheck": this.registerForm.get('pwdCheck').value
    };
    let close=this.modalClose;
    this.userApi.register(reqData,(resp:any)=>close.emit(new UserModel(resp)));
    this.toast.msg('注册成功');

  }

  switchContent(content:any) {
    this.modalContent = content;
  }

  public showModal():void {
    this.isModalShown = true;
  }

  public hideModal():void {
    this.modalClose.emit(false);
  }

  public onHidden():void {
    this.modalClose.emit(false);
  }

  switchSaveCookie() {
    this.saveCookie = !this.saveCookie;
  }

  ngOnInit() {
  }

  showProtocolModal(){
    this.protocolModal.showModal();
  }
}
